#include "../common/dispatch.hpp"
#include "../common/net.hpp"
#include "../common/message.hpp"
#include "../client/requestor.hpp"
#include "../client/RpcCaller.hpp"
#include <thread>


void callback(const Json::Value& result)
{
    ILOG("callback result : %d",result.asInt());
}

int main()
{
    auto requestor = std::make_shared<rpc::client::Requestor>();
    auto caller = std::make_shared<rpc::client::RpcCaller>(requestor);

    auto dispatcher = std::make_shared<rpc::Dispatcher>();
    auto rsp_cb = std::bind(&rpc::client::Requestor::onResponse,requestor.get(),std::placeholders::_1,std::placeholders::_2);
    dispatcher->registerHandler<rpc::BaseMessage>(rpc::Mtype::RSP_RPC,rsp_cb);


    auto client = rpc::CilentFactory::create("127.0.0.1",9090);
    auto msg_cb = std::bind(&rpc::Dispatcher::onMessage,dispatcher.get(),std::placeholders::_1,std::placeholders::_2);
    client->setMessageCallback(msg_cb);
    
    client->connect();

    auto connection = client->connection();
    Json::Value params,result;
    params["num1"] = 11;
    params["num2"] = 22;
    
    bool ret = caller->call(connection,"Add",params,result);
    if(ret != false)
    {
        std::cout << result.asInt() << std::endl;
    }

    params["num1"] = 12;
    params["num2"] = 23;
    rpc::client::RpcCaller::JsonAsyncResponse res_futrue;
    ret = caller->call(connection,"Add",params,res_futrue);
    if(ret != false)
    {
        result = res_futrue.get();
        std::cout << result.asInt() << std::endl;
    }


    params["num1"] = 12;
    params["num2"] = 55;
    ret = caller->call(connection,"Add",params,callback);
    std::this_thread::sleep_for(std::chrono::seconds(5));
    client->shutdown();
    return 0;   
}